package fms.models;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
public class MyBatisSqlConnection {
 
    private static SqlSessionFactory sqlSessionFactory;
 
    static {
        try {
 
            String resource = "SqlMapConfig.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            
            try{
               if (sqlSessionFactory == null) {
                   sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
                   System.out.println("\n \n sqlSessonFactoryBuilder = " + sqlSessionFactory + "\n \n \n");
               }
            }catch(Exception e) {
            	System.out.println("failed to create an instance of SqlSessionFactory");
            	e.printStackTrace();
            }
            
        }catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        }
        catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }
 
    public static SqlSessionFactory getSqlSessionFactory() { 
        return sqlSessionFactory;
    }
}